package com.navidove.appspotlight.model.bo;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import com.navidove.commons.type.EnumType;
import com.navidove.commons.vo.EnhanceEnum;

@Entity
@Table(name = "app_device")
@TypeDefs({ @TypeDef(name = "enum", typeClass = EnumType.class) })
public class AppDevice implements java.io.Serializable {

	public enum DeviceType implements EnhanceEnum {
		IOS("iOS"), ANDROID("Android"), WINDOWS("Windows");
		
		private String name;
		DeviceType (String name) {
			this.name = name;
		}
		
		public String getCode() {
			return this.toString();
		}
		public String getName() {
			return name;
		}
	}
	private static final long serialVersionUID = 1L;

	private Long id;
	private AppItem appItem;
	private DeviceType deviceType;
	private String ver;
	private String dlUrl;
	private Integer dlCnt = 0;
	private SysImage iconImage;
	private SysImage qrcImage;
	private Date pubAt;
	private Boolean enable = Boolean.TRUE;
	private Date crtAt;
	private Date updAt;

	@Id
	@SequenceGenerator(name = "generator", sequenceName = "app_device_id_seq", allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "app_item_id", nullable = false)
	public AppItem getAppItem() {
		return this.appItem;
	}

	public void setAppItem(AppItem appItem) {
		this.appItem = appItem;
	}

	@Column(name = "device_type", nullable = false, length = 50)
	@Type(type="enum")
	public DeviceType getDeviceType() {
		return this.deviceType;
	}

	public void setDeviceType(DeviceType deviceType) {
		this.deviceType = deviceType;
	}

	@Column(name = "ver", nullable = true, length = 50)
	public String getVer() {
		return this.ver;
	}

	public void setVer(String ver) {
		this.ver = ver;
	}

	@Column(name = "dl_url", nullable = true, length = 100)
	public String getDlUrl() {
		return this.dlUrl;
	}

	public void setDlUrl(String dlUrl) {
		this.dlUrl = dlUrl;
	}

	@Column(name = "dl_cnt", nullable = true)
	public Integer getDlCnt() {
		return this.dlCnt;
	}

	public void setDlCnt(Integer dlCnt) {
		this.dlCnt = dlCnt;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "icon_image_id")
	public SysImage getIconImage() {
		return this.iconImage;
	}

	public void setIconImage(SysImage iconImage) {
		this.iconImage = iconImage;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "qrc_image_id")
	public SysImage getQrcImage() {
		return this.qrcImage;
	}

	public void setQrcImage(SysImage qrcImage) {
		this.qrcImage = qrcImage;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "pub_at", nullable = true, length = 29)
	public Date getPubAt() {
		return this.pubAt;
	}

	public void setPubAt(Date pubAt) {
		this.pubAt = pubAt;
	}

	@Column(name = "enable", nullable = false)
	public Boolean getEnable() {
		return this.enable;
	}

	public void setEnable(Boolean enable) {
		this.enable = enable;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "crt_at", length = 29)
	public Date getCrtAt() {
		return this.crtAt;
	}

	public void setCrtAt(Date crtAt) {
		this.crtAt = crtAt;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "upd_at", length = 29)
	public Date getUpdAt() {
		return this.updAt;
	}

	public void setUpdAt(Date updAt) {
		this.updAt = updAt;
	}

	/**
	 * @see java.lang.Object#toString
	 * @return String
	 */
	public String toString() {
		return new ToStringBuilder(this).append("id", getId()).toString();

	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj == this) {
			return true;
		}
		if (obj.getClass() != getClass()) {
			return false;
		}
		AppDevice rhs = (AppDevice) obj;

		return new EqualsBuilder().append(getId(), rhs.getId()).isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getId()).toHashCode();
	}

}
